<template>
  <div class="app-container">
    <el-card class="box-card">
      <template #header>
        <div class="clearfix">
          <span>{{ isEdit ? '编辑文件' : '新增文件' }}</span>
        </div>
      </template>

      <el-form
        ref="storageFormRef"
        :model="form"
        :rules="rules"
        label-width="100px"
        style="max-width: 600px"
      >
        <el-form-item label="文件名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入文件名称" />
        </el-form-item>
        
        <el-form-item label="文件描述" prop="description">
          <el-input
            v-model="form.description"
            type="textarea"
            placeholder="请输入文件描述"
          />
        </el-form-item>
        
        <el-form-item label="文件" prop="file">
          <el-upload
            v-if="!isEdit"
            class="upload-demo"
            :auto-upload="false"
            :on-change="handleFileChange"
            :show-file-list="true"
          >
            <el-button type="primary">选择文件</el-button>
          </el-upload>
          <div v-else>
            <p>文件名称: {{ form.fileName }}</p>
            <p>文件大小: {{ form.fileSize }} bytes</p>
          </div>
        </el-form-item>
        
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="submitForm">保存</el-button>
          <el-button @click="goBack">返回</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getFileDetail, createFile, updateFile } from '@/api/storage'

// 路由相关
const route = useRoute()
const router = useRouter()

// 表单引用
const storageFormRef = ref()

// 是否为编辑模式
const isEdit = ref(false)

// 选中的文件
const selectedFile = ref(null)

// 表单数据
const form = reactive({
  id: undefined,
  name: '',
  description: '',
  fileName: '',
  fileSize: 0,
  status: 1
})

// 表单验证规则
const rules = {
  name: [{ required: true, message: '请输入文件名称', trigger: 'blur' }],
  file: [{ required: true, message: '请选择文件', trigger: 'blur' }]
}

// 处理文件选择
const handleFileChange = (file) => {
  selectedFile.value = file.raw
  form.fileName = file.name
  form.fileSize = file.size
}

// 获取文件详情
const getFileDetailInfo = async (id) => {
  try {
    const response = await getFileDetail(id)
    Object.assign(form, response.data)
  } catch (error) {
    ElMessage.error('获取文件信息失败')
  }
}

// 提交表单
const submitForm = async () => {
  if (!storageFormRef.value) return
  
  await storageFormRef.value.validate((valid) => {
    if (valid) {
      if (isEdit.value) {
        // 编辑模式
        updateFile(form)
          .then(() => {
            ElMessage.success('保存成功')
            goBack()
          })
          .catch(() => {
            ElMessage.error('保存失败')
          })
      } else {
        // 新增模式
        if (!selectedFile.value) {
          ElMessage.error('请选择文件')
          return
        }
        
        // 这里需要实现文件上传逻辑
        createFile(form)
          .then(() => {
            ElMessage.success('保存成功')
            goBack()
          })
          .catch(() => {
            ElMessage.error('保存失败')
          })
      }
    }
  })
}

// 返回上一页
const goBack = () => {
  router.push('/storage')
}

// 初始化
onMounted(() => {
  // 判断是否为编辑模式
  if (route.params.id) {
    isEdit.value = true
    getFileDetailInfo(route.params.id)
  }
})
</script>

<style scoped>
.app-container {
  padding: 20px;
}
.upload-demo {
  width: 100%;
}
</style>